package com.hy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.admin.pojo.RegistrationGrowth;
import com.hy.admin.pojo.RegistrationInfo;
import com.hy.admin.pojo.UserActivity;
import com.hy.exception.ZhentaoException;
import com.hy.mapper.UserActivityMapper;
import com.hy.service.UserActivityService;
import com.hy.util.common.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity>
        implements UserActivityService {

    @Autowired
    private UserActivityMapper userActivityMapper;

    @Override
    public double[] calculateTotals() {
        try {
            // 直接从用户表统计各类型用户数量
            Integer hospitalCount = userActivityMapper.countUsersByType(1); // 医院数量
            Integer doctorCount = userActivityMapper.countUsersByType(2);   // 医生数量
            Integer patientCount = userActivityMapper.countUsersByType(3);  // 患者数量

            // 处理null值
            hospitalCount = hospitalCount == null ? 0 : hospitalCount;
            doctorCount = doctorCount == null ? 0 : doctorCount;
            patientCount = patientCount == null ? 0 : patientCount;

            log.info("统计数据 - 医院:{}, 医生:{}, 患者:{}", 
                    hospitalCount, doctorCount, patientCount);

            return new double[]{hospitalCount, doctorCount, patientCount};
        } catch (Exception e) {
            log.error("计算统计数据失败", e);
            throw new ZhentaoException(ResultCodeEnum.SYSTEM_ERROR, "计算统计数据失败");
        }
    }

    @Override
    public List<RegistrationInfo> getRegistrationInfos() {
        try {
            List<RegistrationInfo> infos = userActivityMapper.selectRegistrationInfos();
            log.info("获取注册信息成功，数据条数: {}", infos.size());
            return infos;
        } catch (Exception e) {
            log.error("获取注册信息失败", e);
            throw new ZhentaoException(ResultCodeEnum.SYSTEM_ERROR, "获取注册信息失败");
        }
    }

    @Override
    public List<UserActivity> getUserActivities() {
        try {
            List<UserActivity> activities = userActivityMapper.selectUserActivities();
            log.info("获取用户活跃度成功，数据条数: {}", activities.size());
            return activities;
        } catch (Exception e) {
            log.error("获取用户活跃度失败", e);
            throw new ZhentaoException(ResultCodeEnum.SYSTEM_ERROR, "获取用户活跃度失败");
        }
    }

    @Override
    public List<RegistrationGrowth> getRegistrationGrowths() {
        try {
            List<RegistrationGrowth> growths = userActivityMapper.selectRegistrationGrowths();
            log.info("获取注册增长数据成功，数据条数: {}", growths.size());
            return growths;
        } catch (Exception e) {
            log.error("获取注册增长数据失败", e);
            throw new ZhentaoException(ResultCodeEnum.SYSTEM_ERROR, "获取注册增长数据失败");
        }
    }

    /**
     * 计算注册信息表中注册数量总和
     */
    private int calculateRegistrationCount(List<RegistrationInfo> registrationInfos) {
        if (registrationInfos == null) {
            return 0;
        }
        return registrationInfos.stream()
                .filter(info -> info != null && info.getCount() != null)
                .mapToInt(RegistrationInfo::getCount)
                .sum();
    }

    /**
     * 计算用户活跃度表中活跃度总和
     */
    private double calculateActivityRate(List<UserActivity> userActivities) {
        if (userActivities == null) {
            return 0.0;
        }
        return userActivities.stream()
                .filter(activity -> activity != null && activity.getActivityRate() != null)
                .map(UserActivity::getActivityRate)
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .doubleValue();
    }

    /**
     * 计算注册用户增长表中注册用户数总和
     */
    private int calculateRegistrationGrowth(List<RegistrationGrowth> registrationGrowths) {
        if (registrationGrowths == null) {
            return 0;
        }
        return registrationGrowths.stream()
                .filter(growth -> growth != null && growth.getRegistrationCount() != null)
                .mapToInt(RegistrationGrowth::getRegistrationCount)
                .sum();
    }
}
